<?php
/**
* object-oriented file system manipulation as nodes.
* This file belongs to a webappkit package (kit)
* @link http://webappkit.net
* @package pathnodes
* @subpackage yaml
* @author J.Ducastel <nospam0@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* registers as pathnode descendant
*/
pathnode::heritage('yamlfilenode',20);
/**
* yaml file handler
* @link http://en.wikipedia.org/wiki/YAML
* @use dataPath for data path syntax
* @uses Spyc for yaml conversion
*/
class yamlFileNode extends fileNode {

    /**
    * loads file content
    * @access public
    * @return bool
    */
    function load() {
        if ($this->sync)
            return true;
        if (!class_exists('spyc')) {
            trigger_error('Missing required class "spyc"',E_USER_WARNING);
            // exit;
            return false;
        }
        $str=$this->read(); //echo $str;
        $data=spyc::yamlLoad($str);
        if (is_array($data)) {
            $this->data=&$data;
            $this->sync=true;
        }
        return true;
    }/**/
    
    /**
    * save data to yaml file
    * @access public
    * @return bool
    */
    function save() {
        if ($this->sync)
            // not needed
            return true;
        // is writable ?
        if (!$this->isWritable())
            return false;
        // rewinding handle
        $this->toStart();
        $this->erase();
        // converting data to yaml syntax
        $yaml=spyc::yamlDump($this->data,2,255);
        // writing to file
        if ($this->write($yaml))
            $this->sync=true;
        return $this->sync;
    }/**/
    
    /**
    * are file and data synchronized ?
    * @access public
    * @return bool
    */
    function isUpToDate() {
        return $this->sync;
    }/**/
    
    /**
    * return data at targeted path.
    * @see dataPath for syntax
    * @param string $path
    * @access public
    * @return mixed
    */
    function getData($path='') {
        if (!$this->sync)
            $this->load();
        return datapath::get($this->data,$path);
    }/**/
    
    /**
    * sets some data
    * @see dataPath
    * @param mixed &$value
    * @param string $path
    * @access public
    * @return bool
    */
    function setData($value,$path='') {
        $set=datapath::set($this->data,$value,$path);
        if ($set)
            $this->sync=false;
        return $set;
    }/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
    /**
    * @var array content array
    */
    var $data=array();
    /**
    * @var bool are file and data synchronized
    */
    var $sync=false;
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
    /**
    * detects if path applies to self class.
    * subclasses HAVE to implement this method
    * @param string $path
    * @access protected
    * @return bool
    */
    function selfDetector($path) {
        return ereg('\.yml$',$path);
    }/**/
}
?>
